﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MovieRental.Entities;
using MovieRental.SqlRepositoryLibrary.Helpers;

namespace MovieRental.SqlRepositoryLibrary.Converters
{
    public class CustomerConverter : BaseConverter<CustomerInfo, Guid>
    {
        protected override SqlEntityMapping SetUpMapping()
        {
            var mapping = new SqlEntityMapping("Customers", "Id");
            mapping.AddProperty("Id", "Id", SqlDbType.UniqueIdentifier);
            mapping.AddProperty("FirstName", "FirstName", SqlDbType.NVarChar);
            mapping.AddProperty("LastName", "LastName", SqlDbType.NVarChar);
            mapping.AddProperty("Email", "Email", SqlDbType.NVarChar);
            mapping.AddProperty("PhoneNo", "PhoneNo", SqlDbType.NVarChar);
            return mapping;
        }

        protected override void SetUpPropertyValues(CustomerInfo entity)
        {
            SetProperty("Id", entity.Id);
            SetProperty("FirstName", entity.FirstName);
            SetProperty("LastName", entity.LastName);
            SetProperty("Email", entity.Email);
            SetProperty("PhoneNo", entity.PhoneNo);
        }

        public override CustomerInfo ConvertFromDataReader(SqlDataReader reader)
        {
            Guid id = (Guid)GetValueForProperty(reader, "Id");
            CustomerInfo customer = new CustomerInfo(id);
            customer.FirstName = (string) GetValueForProperty(reader, "FirstName");
            customer.LastName = (string) GetValueForProperty(reader, "LastName");
            customer.Email = (string)GetValueForProperty(reader, "Email");
            customer.PhoneNo = (string)GetValueForProperty(reader, "PhoneNo");
            return customer;
        }
    }
}
